package com.java.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.java.vo.Master;


@Repository
public class MasterDao {

	private Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private JdbcTemplate jdbcTemplate;


	public int getUserPassword(String name, String password) {
		final String sql = "SELECT id FROM master WHERE name =  ? AND password = ? ";
		try {
			return jdbcTemplate.queryForObject(sql, new Object[] { name, password }, Integer.class);
		} catch (Exception e) {
			logger.error("not exist user by name and password = " + name + "|" + e.getLocalizedMessage());
			return -1;
		}
	}
	
	public Master getUserPw(String name, String password) {
		final String sql = "SELECT * FROM master WHERE name =  ? AND password = ? ";
		try {
			return jdbcTemplate.queryForObject(sql, new Object[] { name, password }, new RowMapper<Master>() {

				public Master mapRow(ResultSet rs, int rowNum) throws SQLException {
					Master master = new Master();
					master.setId(rs.getInt("id"));
					master.setName(rs.getString("name"));
					master.setPassword(rs.getString("password"));
					master.setMasterId(rs.getInt("masterId"));
					master.setMasterName(rs.getString("masterName"));
					return master;
				}
			});
		} catch (Exception e) {
			logger.error("not exist user by name and password = " + name + "|" + e.getLocalizedMessage());
			return null;
		}
	}
	
	public Master getUserByName(String name) {
		final String sql = "SELECT * FROM master WHERE name =  ?";
		try {
			return jdbcTemplate.queryForObject(sql, new Object[] { name }, new RowMapper<Master>() {

				public Master mapRow(ResultSet rs, int rowNum) throws SQLException {
					Master master = new Master();
					master.setId(rs.getInt("id"));
					master.setName(rs.getString("name"));
					master.setPassword(rs.getString("password"));
					master.setMasterId(rs.getInt("masterId"));
					master.setMasterName(rs.getString("masterName"));
					return master;
				}
			});
		} catch (DataAccessException e) {
			logger.error("not exist master by name = " + name + "|" + e.getLocalizedMessage());
			return null;
		}
	}
	
	public Master getUserById(Integer id) {
		final String sql = "SELECT * FROM master WHERE id =  ?";
		try {
			return jdbcTemplate.queryForObject(sql, new Object[] { id }, new RowMapper<Master>() {

				public Master mapRow(ResultSet rs, int rowNum) throws SQLException {
					Master master = new Master();
					master.setId(rs.getInt("id"));
					master.setName(rs.getString("name"));
					master.setPassword(rs.getString("password"));
					master.setMasterId(rs.getInt("masterId"));
					master.setMasterName(rs.getString("masterName"));
					return master;
				}
			});
		} catch (DataAccessException e) {
			logger.error("not exist master by name = " + id + "|" + e.getLocalizedMessage());
			return null;
		}
	}
	
}
